from sqlalchemy.orm import Session
from init_rabc_db import User, UserAttribute, Role, UserRole, Permission, RolePermission, OperationLog, LogOperationType, LogDetail
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from contextlib import contextmanager
from init_rabc_db import DATABASE_URL

engine = create_engine(DATABASE_URL, pool_pre_ping=True)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# FastAPI依赖注入用
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

# User DAO
class UserDAO:
    def __init__(self, session: Session):
        self.session = session

    def get_by_id(self, user_id):
        return self.session.query(User).get(user_id)

    def get_by_username(self, username):
        return self.session.query(User).filter_by(username=username).first()

    def create(self, user: User):
        self.session.add(user)
        self.session.commit()
        return user

    def update(self, user: User):
        self.session.commit()
        return user

    def delete(self, user: User):
        self.session.delete(user)
        self.session.commit()

# UserAttribute DAO
class UserAttributeDAO:
    def __init__(self, session: Session):
        self.session = session

    def get(self, user_id, attr_key):
        return self.session.query(UserAttribute).filter_by(user_id=user_id, attr_key=attr_key).first()

    def create(self, attr: UserAttribute):
        self.session.add(attr)
        self.session.commit()
        return attr

    def update(self, attr: UserAttribute):
        self.session.commit()
        return attr

    def delete(self, attr: UserAttribute):
        self.session.delete(attr)
        self.session.commit()

# Role DAO
class RoleDAO:
    def __init__(self, session: Session):
        self.session = session

    def get_by_id(self, role_id):
        return self.session.query(Role).get(role_id)

    def get_by_name(self, role_name):
        return self.session.query(Role).filter_by(role_name=role_name).first()

    def create(self, role: Role):
        self.session.add(role)
        self.session.commit()
        return role

    def update(self, role: Role):
        self.session.commit()
        return role

    def delete(self, role: Role):
        self.session.delete(role)
        self.session.commit()

# UserRole DAO
class UserRoleDAO:
    def __init__(self, session: Session):
        self.session = session

    def get(self, user_id, role_id):
        return self.session.query(UserRole).filter_by(user_id=user_id, role_id=role_id).first()

    def create(self, user_role: UserRole):
        self.session.add(user_role)
        self.session.commit()
        return user_role

    def delete(self, user_role: UserRole):
        self.session.delete(user_role)
        self.session.commit()

# Permission DAO
class PermissionDAO:
    def __init__(self, session: Session):
        self.session = session

    def get_by_id(self, permission_id):
        return self.session.query(Permission).get(permission_id)

    def create(self, permission: Permission):
        self.session.add(permission)
        self.session.commit()
        return permission

    def update(self, permission: Permission):
        self.session.commit()
        return permission

    def delete(self, permission: Permission):
        self.session.delete(permission)
        self.session.commit()

# RolePermission DAO
class RolePermissionDAO:
    def __init__(self, session: Session):
        self.session = session

    def get(self, role_id, permission_id):
        return self.session.query(RolePermission).filter_by(role_id=role_id, permission_id=permission_id).first()

    def create(self, role_permission: RolePermission):
        self.session.add(role_permission)
        self.session.commit()
        return role_permission

    def delete(self, role_permission: RolePermission):
        self.session.delete(role_permission)
        self.session.commit()

# OperationLog DAO
class OperationLogDAO:
    def __init__(self, session: Session):
        self.session = session

    def get_by_id(self, log_id):
        return self.session.query(OperationLog).get(log_id)

    def create(self, log: OperationLog):
        self.session.add(log)
        self.session.commit()
        return log

    def delete(self, log: OperationLog):
        self.session.delete(log)
        self.session.commit()

# LogOperationType DAO
class LogOperationTypeDAO:
    def __init__(self, session: Session):
        self.session = session

    def get_by_id(self, type_id):
        return self.session.query(LogOperationType).get(type_id)

    def get_by_code(self, type_code):
        return self.session.query(LogOperationType).filter_by(type_code=type_code).first()

    def create(self, log_type: LogOperationType):
        self.session.add(log_type)
        self.session.commit()
        return log_type

    def delete(self, log_type: LogOperationType):
        self.session.delete(log_type)
        self.session.commit()

# LogDetail DAO
class LogDetailDAO:
    def __init__(self, session: Session):
        self.session = session

    def get(self, log_id, detail_key):
        return self.session.query(LogDetail).filter_by(log_id=log_id, detail_key=detail_key).first()

    def create(self, detail: LogDetail):
        self.session.add(detail)
        self.session.commit()
        return detail

    def update(self, detail: LogDetail):
        self.session.commit()
        return detail

    def delete(self, detail: LogDetail):
        self.session.delete(detail)
        self.session.commit() 